/**
 * @Description:  # TODO 页面打开,发请求,拿数据,渲染
 * @Author: 文渊
 * @Date: 2023-09-04 10:32:13
 */
window.onload = function () {
    //获取登录的用户权限
    const level = JSON.parse(localStorage.getItem("userInfo"))[0].level

    const axios = ajax()
    axios({
        url: "/admin/queryall?level=" + level,
        headers: { token: localStorage.getItem("token") }
    })
        .then(res => {
            //就是没有权限
            if (res.msg.length == 0) {
                //把权限存储起来
                localStorage.setItem("level", 1)
                //不拥有这个菜单
                document.getElementById("adminLevel").remove();
            } else {
                // console.log(res);
                localStorage.setItem("adminInfo", JSON.stringify(res.msg))
                //渲染工作
                const admintBody = document.getElementById("admintBody")
                //清空
                admintBody.innerHTML = "";
                res.msg.forEach((item, index) => {
                    let str = `
                <tr align="center">
                    <td>${index + 1}</td>
                    <td>${item.username}</td>
                    <td>${item.nickname}</td>
                    <td>
                        <img width="40px" height="40px" src="${item.url}">    
                    </td>
                    <td>${item.level == 0 ? '超级管理员' : '管理员'}</td>
                    <td>
                        <button class="btn btn-danger btn-sm" data-id='${item.aid}'>删除</button>
                        <button class="btn btn-warning btn-sm" data-id='${item.aid}' data-bs-toggle="modal" data-bs-target="#adminModal">修改</button>
                    </td>
                </tr>
                `
                    admintBody.innerHTML += str;
                })
            }
        })
}

/**
 * @Description:  # TODO 添加或者修改管理员信息
 * @Author: 文渊
 * @Date: 2023-09-04 10:42:54
 */
function submitadmin() {
    const title = localStorage.getItem("toadminPage")
    if(title == '添加'){
        const username = document.getElementById("ausername").value.trim();
        const password = document.getElementById("apassword").value.trim();
        const nickname = document.getElementById("anickname").value.trim();
        const url = document.getElementById("imgEle").src
        const level = document.getElementById("alevel").value.trim();
        //校验
        if (!(username.length && password.length && nickname.length)) return alert("请输入完整的信息")
        if (level == '-1') return alert("管理员信息部能为空!")
    
        //发送ajax
        const axios = ajax();
        axios({
            url: "/admin/add",
            data: { username, password, nickname, url, level },
            method: "post",
            headers: { token: localStorage.getItem("token") }
        })
        .then(res => {
            location.reload();
        })
    }else{
        const username = document.getElementById("ausername").value.trim();
        const password = document.getElementById("apassword").value.trim();
        const nickname = document.getElementById("anickname").value.trim();
        const url = document.getElementById("imgEle").src
        const level = document.getElementById("alevel").value.trim();
        const id = document.getElementById("aid").value;
        //校验
        if (!(username.length && password.length && nickname.length)) return alert("请输入完整的信息")
        if (level == '-1') return alert("管理员信息部能为空!")
        //发送axios
        const axios = ajax();
        axios({
            url:"/admin/edit",
            method:"post",
            data:{
                id,username,password,nickname,url,level
            },
            headers:{token:localStorage.getItem("token")}
        })
        .then(res=>{
            location.reload();
        })
    }
}

/**
 * @Description:  # TODO 删除获取修改
 * @Author: 文渊
 * @Date: 2023-09-04 11:20:44
 */
const admintBody = document.getElementById("admintBody")
admintBody.addEventListener("click", e => {
    //删除操作
    if (e.target.className.includes('danger')) {
        const result = confirm("您确定要删除吗?")
        if (result) {
            const id = e.target.dataset.id
            //发送ajax
            const axios = ajax()
            axios({
                url: "/admin/remove",
                data: { id },
                method: "post",
                headers: { token: localStorage.getItem("token") }
            })
                .then(res => {
                    window.location.reload();
                })
        }
    }
    //修改操作
    if (e.target.className.includes('warning')) {
        //存储当前模态框的标题
        localStorage.setItem("toadminPage", "修改")
        //修改模态框的标题
        document.getElementById("admintitle").innerHTML = "修改"
        //获取id
        const id = e.target.dataset.id
        //发送ajax
        const axios = ajax()
        axios({
            url: "/admin/queryone?id=" + id,
            headers: { token: localStorage.getItem("token") }
        })
        .then(res => {
            document.getElementById("ausername").value = res.msg[0].username
            document.getElementById("apassword").value = res.msg[0].password
            document.getElementById("anickname").value = res.msg[0].nickname
            document.getElementById("imgEle").src = res.msg[0].url
            document.getElementById("alevel").value = res.msg[0].level
            document.getElementById("aid").value = res.msg[0].aid
        })
    }
})

//点击添加按钮
function showAdd(){
    localStorage.setItem('toadminPage','添加');
    document.getElementById('admintitle').innerHTML = '添加'
    //清空表单信息
    document.getElementById("ausername").value = "";
    document.getElementById("apassword").value = "";
    document.getElementById("anickname").value = "";
    document.getElementById("imgEle").src = "http://s08vr7vdh.hn-bkt.clouddn.com/avatar.png"
    document.getElementById("alevel").value = "";
}